/**
 * 
 */
package com.googlecode.agiletools.dailystandup.action;

import java.util.Calendar;
import java.util.Date;
import java.util.HashMap;
import java.util.Iterator;
import java.util.LinkedList;
import java.util.List;

import org.apache.commons.lang.time.DateUtils;

import com.googlecode.agiletools.dailystandup.DailyStandup;
import com.googlecode.agiletools.dailystandup.DailyStandupManager;
import com.opensymphony.xwork2.Action;

/**
 * @author Administrator
 * 
 */
public class DailyStandupListAction {

    private DailyStandupManager dailyStandupManager;

    private Date from;

    private Date to;

    private Date focus;

    private List<DateSummary> dailyStandups;

    /**
     * @return the dailyStandups
     */
    public List<DateSummary> getDailyStandups() {
	return dailyStandups;
    }

    /**
     * @return the focus
     */
    public Date getFocus() {
	return focus;
    }

    /**
     * @return the dateFrom
     */
    public Date getFrom() {
	return from;
    }

    /**
     * @return the dateTo
     */
    public Date getTo() {
	return to;
    }

    /**
     * @param dailyStandupList
     * @return
     */
    private List<DateSummary> groupByDate(List<DailyStandup> dailyStandupList) {
	HashMap<Date, DateSummary> map = new HashMap<Date, DateSummary>();
	if (dailyStandups == null) {
	    dailyStandups = new LinkedList<DateSummary>();
	} else {
	    for (DateSummary summary : dailyStandups) {
		map.put(summary.getDate(), summary);
	    }
	}
	for (DailyStandup dailyStandup : dailyStandupList) {
	    Date date = dailyStandup.getDate();

	    date = DateUtils.truncate(date, Calendar.DATE);

	    DateSummary dateSummary = null;
	    if (map.containsKey(date)) {
		dateSummary = map.get(date);
	    } else {
		dateSummary = new DateSummary();
		dateSummary.setDate(date);
		map.put(date, dateSummary);
		dailyStandups.add(dateSummary);
	    }
	    dateSummary.getDailyStandups().add(dailyStandup);
	}
	return dailyStandups;
    }

    public String last() {
	focus = DateUtils.addDays(focus, -7);
	return week();
    }

    public String list() {
	List<DailyStandup> dailyStandupList = dailyStandupManager
		.find(from, to);
	dailyStandups = groupByDate(dailyStandupList);
	return Action.SUCCESS;
    }

    public String next() {
	focus = DateUtils.addDays(focus, 7);
	return week();
    }

    /**
     * @param dailyStandupManager
     *            the dailyStandupManager to set
     */
    public void setDailyStandupManager(DailyStandupManager dailyStandupManager) {
	this.dailyStandupManager = dailyStandupManager;
    }

    /**
     * @param focus
     *            the focus to set
     */
    public void setFocus(Date focus) {
	this.focus = focus;
    }

    /**
     * @param dateFrom
     *            the dateFrom to set
     */
    public void setFrom(Date dateFrom) {
	this.from = dateFrom;
    }

    /**
     * @param dateTo
     *            the dateTo to set
     */
    public void setTo(Date dateTo) {
	this.to = dateTo;
    }

    @SuppressWarnings("unchecked")
    public String week() {
	if (focus == null) {
	    focus = Calendar.getInstance().getTime();
	}
	Iterator iterator = DateUtils.iterator(focus,
		DateUtils.RANGE_WEEK_MONDAY);

	dailyStandups = new LinkedList<DateSummary>();

	for (int i = 0; i < 5 & iterator.hasNext(); i++) {
	    DateSummary summary = new DateSummary();
	    summary.setDate(((Calendar) iterator.next()).getTime());
	    dailyStandups.add(summary);
	}

	from = dailyStandups.get(0).getDate();
	to = dailyStandups.get(dailyStandups.size() - 1).getDate();

	List<DailyStandup> dailyStandupList = dailyStandupManager
		.find(from, to);

	dailyStandups = groupByDate(dailyStandupList);
	return Action.SUCCESS;
    }

}
